package com.grep.extra.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import com.grep.base.db.DBUtils;
import com.grep.extra.dao.BaseDao;
import com.grep.extra.factory.data.impl.ContentParseField;

public class ContentParseFieldDao {
	
	BaseDao baseDao = new BaseDaoImpl();
	
	//添加
	public boolean AddContentParseField(String insetsql){
		return baseDao.insert(insetsql);
	}
	
	//根据ID，得到所属内容
	public ContentParseField getContParseFieldforID(String ContParseFieldID ){
		
		ContentParseField cpf = new ContentParseField();
		
		Connection conn = null;
		Statement stat = null;
		ResultSet result = null;
		
		String sql = "select * from content_parse_field where id='"+ContParseFieldID+"'";
		
		try {
			conn = DBUtils.getConnection();
			stat = conn.createStatement();
			result = stat.executeQuery(sql);
			if(result.next()){		
				cpf.setId(result.getString("id"));
				cpf.setFieldName(result.getString("field_name"));
				cpf.setStartRegex(result.getString("start_regex"));
				cpf.setEndRegex(result.getString("end_regex"));
			}
			
		}catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtils.close(result);
			DBUtils.close(stat);
			DBUtils.close(conn);
		}
		
		return cpf;
	}
	
	//根据ContentParseID,得到所属ContentParseField数据
	public List<ContentParseField> getFieldForContentParseID(String contentParseID,int start,int pagesize){
		
		List<ContentParseField> list = new ArrayList<ContentParseField>();
		
		String sql = "select * from content_parse_field where content_parse_id='"+contentParseID+"'order by id limit " + start + "," + pagesize;
		Connection conn = null;
		Statement stat = null;
		ResultSet result = null;
		
		try {
			conn = DBUtils.getConnection();
			stat = conn.createStatement();
			result = stat.executeQuery(sql);
			while (result.next()) {				
				ContentParseField cpf = new ContentParseField();
				
				cpf.setId(result.getString("id"));
				cpf.setFieldName(result.getString("field_name"));
				cpf.setStartRegex(result.getString("start_regex"));
				cpf.setEndRegex(result.getString("end_regex"));
				list.add(cpf);
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtils.close(result);
			DBUtils.close(stat);
			DBUtils.close(conn);
		}
		return list;
		
	}
	
	public int getFieldCountByContentParseID(String contentParseID){
		
		int count=0;
		Connection conn = null;
		Statement stat = null;
		ResultSet result = null;
		String countsql = "select count(*) from content_parse_field where content_parse_id='"+contentParseID+"'";
		try {
			conn = DBUtils.getConnection();
			stat = conn.createStatement();
			result = stat.executeQuery(countsql);
			if (result.next()) {
				count = result.getInt(1);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(result);
			DBUtils.close(stat);
			DBUtils.close(conn);
		}

		return count;
	}
	
	
	
		
	public boolean modificContParseField(String modificsql){
		return baseDao.insert(modificsql);
	}
	
	public boolean deleteContParseField(String deletesql){
		return baseDao.insert(deletesql);
	}
			
}
